﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace InnoWeb.CEND.Web.Framework
{
    public class ImageUploader
    {
        public static bool ImageSave(string thumbPath, string fullPath, HttpPostedFileBase model, string fileName, bool IsSlider = false)
        {
            try
            {
                //string fileName = Guid.NewGuid().ToString();
                if (!IsSlider)
                {
                    ResizeAndSave(thumbPath, fileName, model.InputStream, 100, 100);
                    ResizeAndSave(fullPath, fileName, model.InputStream, 250, 250);    
                }
                else
                {
                    FixedSize(fullPath, fileName, model.InputStream, 989, 450);
                }
                

                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        private static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int width, int height)
        {
            using (var srcImage = Image.FromStream(imageBuffer))
            using (var newImage = new Bitmap(width, height))
            using (var graphics = Graphics.FromImage(newImage))
            using (var stream = new MemoryStream())
            {
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                graphics.DrawImage(srcImage, new Rectangle(0, 0, width, height));

                string savePathLast = Path.Combine(savePath, fileName + ".jpg");
                newImage.Save(savePathLast, ImageFormat.Jpeg);
            }


            //int newWidth, newHeight;
            //Image image = Image.FromStream(imageBuffer);
            //int oldWidth = image.Width;
            //int oldHeight = image.Height;
            //Bitmap newImage;
            //if (makeItSquare)
            //{
            //    int smallerSide = oldWidth >= oldHeight ? oldHeight : oldWidth;
            //    double coeficient = maxSideSize / (double)smallerSide;
            //    newWidth = Convert.ToInt32(coeficient * oldWidth);
            //    newHeight = Convert.ToInt32(coeficient * oldHeight);
            //    Bitmap tempImage = new Bitmap(image, newWidth, newHeight);
            //    int cropX = (newWidth - maxSideSize) / 2;
            //    int cropY = (newHeight - maxSideSize) / 2;
            //    newImage = new Bitmap(maxSideSize, maxSideSize);
            //    Graphics tempGraphic = Graphics.FromImage(newImage);
            //    tempGraphic.SmoothingMode = SmoothingMode.AntiAlias;
            //    tempGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //    tempGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
            //    tempGraphic.DrawImage(tempImage, new Rectangle(0, 0, maxSideSize, maxSideSize), cropX, cropY, maxSideSize, maxSideSize, GraphicsUnit.Pixel);
            //}
            //else
            //{
            //    int maxSide = oldWidth >= oldHeight ? oldWidth : oldHeight;

            //    if (maxSide > maxSideSize)
            //    {
            //        double coeficient = maxSideSize / (double)maxSide;
            //        newWidth = Convert.ToInt32(coeficient * oldWidth);
            //        newHeight = Convert.ToInt32(coeficient * oldHeight);
            //    }
            //    else
            //    {
            //        newWidth = oldWidth;
            //        newHeight = oldHeight;
            //    }
            //    newImage = new Bitmap(image, newWidth, newHeight);
            //}

            //string savePathLast = Path.Combine(savePath, fileName + ".jpg");
            ////newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg);
            //newImage.Save(savePathLast, ImageFormat.Jpeg);
            //image.Dispose();
            //newImage.Dispose();
        }

        static void FixedSize(string savePath, string fileName, Stream imageBuffer, int Width, int Height)
        {
            var imgPhoto = Image.FromStream(imageBuffer);
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)Width / (float)sourceWidth);
            nPercentH = ((float)Height / (float)sourceHeight);
            if (nPercentH < nPercentW)
            {
                nPercent = nPercentH;
                destX = System.Convert.ToInt16((Width -
                              (sourceWidth * nPercent)) / 2);
            }
            else
            {
                nPercent = nPercentW;
                destY = System.Convert.ToInt16((Height -
                              (sourceHeight * nPercent)) / 2);
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(Width, Height,
                              PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                             imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.Clear(Color.White);
            grPhoto.InterpolationMode =
                    InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();

            string savePathLast = Path.Combine(savePath, fileName + ".jpg");
            bmPhoto.Save(savePathLast, ImageFormat.Jpeg);

            //return bmPhoto;
        }

    }

    public class VideoUploader
    {
        public static bool VideoSave(string fullPath, HttpPostedFileBase model, string fileName)
        {
            try
            {
                model.SaveAs(fullPath + fileName + ".avi");

                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
    }

    public class FileUploader
    {
        public static bool FileSave(string fullPath, HttpPostedFileBase model, string fileName)
        {
            try
            {
                model.SaveAs(fullPath + fileName + ".pdf");

                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }


    }
}
